//
//  GiftPackViewController.m
//  GameBaby
//
//  Created by keyZhang on 13-12-19.
//  Copyright (c) 2013年 keyZhang. All rights reserved.
//

#import "GiftPackViewController.h"
#import "DataService.h"
#import "GiftCellModel.h"

@interface GiftPackViewController ()

@end

@implementation GiftPackViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UILabel *t = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
        t.font = [UIFont systemFontOfSize:16];
        t.textColor = [UIColor blackColor];
        t.backgroundColor = [UIColor clearColor];
        t.textAlignment = UITextAlignmentCenter;
        t.text = @"礼包";
        self.navigationItem.titleView = t;

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _xsDataList = [[NSMutableArray alloc] init];
    _jhDataList = [[NSMutableArray alloc] init];
    _jfDataList = [[NSMutableArray alloc] init];
    self.xsTbView.refreshDelegate = self;
    self.jhTbView.refreshDelegate = self;
    self.jfTbView.refreshDelegate = self;
    
    __block GiftPackViewController *this = self;

    self.xsTbView.finishBlock = ^{
        [this _loadXsMoreData];
    };
    
    self.jhTbView.finishBlock = ^{
        [this _loadJhMoreData];
    };
    
    self.jfTbView.finishBlock = ^{
        [this _loadJfMoreData];
    };
    
    [self loadXsData];
    [self loadJhData];
    [self loadJfData];
    
    UISwipeGestureRecognizer *leftswipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    leftswipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:leftswipeRecognizer];
    
    UISwipeGestureRecognizer *rightswipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    rightswipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:rightswipeRecognizer];
    
}

- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSInteger tag_value = [[[self.changeView subviews] lastObject] tag];
        if (tag_value <= 20) {
            UIView *chooseView = [self.changeView viewWithTag:tag_value + 10];
            UIView *backView = [self.changeView viewWithTag:tag_value];
//            [UIView animateWithDuration:0.35 animations:^{
//                [self.changeView bringSubviewToFront:chooseView];
//                self.arrowImgView.left = self.arrowImgView.left + 70;
////            }];
            chooseView.left = kScreenWidth;
            
            UIViewAnimationTransition transition = UIViewAnimationTransitionNone;
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationCurve:UIViewAnimationCurveLinear];
            [UIView setAnimationDuration:0.5];
            [UIView setAnimationTransition:transition forView:self.changeView cache:YES];
            backView.right = 0;
            chooseView.left = 0;
            [self.changeView bringSubviewToFront:chooseView];
            self.arrowImgView.left = self.arrowImgView.left + 70;
            
            [UIView commitAnimations];
            
//            [UIView animateWithDuration:0.7 delay:0 options:0 animations:^(void){
//                [self.changeView bringSubviewToFront:chooseView];
//                self.arrowImgView.left = self.arrowImgView.left + 70;
//            } completion:^(BOOL finished)
//             {  
//                 
//             }];
        }
    }
    
    if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
        NSInteger tag_value = [[[self.changeView subviews] lastObject] tag];
        if (tag_value > 10) {
            UIView *chooseView = [self.changeView viewWithTag:tag_value - 10];
            UIView *backView = [self.changeView viewWithTag:tag_value];
//            [UIView animateWithDuration:0.35 animations:^{
//                [self.changeView bringSubviewToFront:chooseView];
//                self.arrowImgView.left = self.arrowImgView.left - 70;
//            }];
            
            chooseView.right = 0;
            
            UIViewAnimationTransition transition = UIViewAnimationTransitionNone;
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationCurve:UIViewAnimationCurveLinear];
            [UIView setAnimationDuration:0.5];
            [UIView setAnimationTransition:transition forView:self.changeView cache:YES];
            //
            chooseView.left = 0;
            backView.left = kScreenWidth;
            [self.changeView bringSubviewToFront:chooseView];
            self.arrowImgView.left = self.arrowImgView.left - 70;
            
            [UIView commitAnimations];
        }
    }
}

- (void)loadXsData
{
    NSString *urlStr = [NSString stringWithFormat:@"http://m.a.ewoka.com/number/cardlist/1/%lu",(long)self.xsDataList.count/10+1];
    __weak GiftPackViewController *this = self;
    [DataService requestWithURL:urlStr
                         params:nil
                     httpMethod:@"GET"
                    finishBlock:^(id result) {
                        [this loadXsDataFinished:result];
                    }];

}

- (void)_loadXsMoreData
{
    _xsDataList = [NSMutableArray array];
    [self loadXsData];
}   

- (void)loadXsDataFinished:(NSDictionary *)result
{
    NSArray *array = [result objectForKey:@"list"];
    if (array.count < 10) {
        self.xsTbView.isMore = NO;
    }else
    {
        self.xsTbView.isMore = YES;
    }
    NSMutableArray *mArray = [[NSMutableArray alloc] initWithCapacity:array.count];
    for (NSDictionary *dic in array) {
        GiftCellModel *model = [[GiftCellModel alloc] initContentWith:dic];
        [mArray addObject:model];
    }
    [_xsDataList addObjectsFromArray:mArray];
    self.xsTbView.dataList = self.xsDataList;
    [self.xsTbView reloadData];
    [self.xsTbView doneLoadingTableViewData];
}

- (void)loadJhData
{
    NSString *urlStr = [NSString stringWithFormat:@"http://m.a.ewoka.com/number/cardlist/2/%d",self.jhDataList.count/10+1];
    __weak GiftPackViewController *this = self;

    [DataService requestWithURL:urlStr
                         params:nil
                     httpMethod:@"GET"
                    finishBlock:^(id result) {
                        [this loadJhDataFinished:result];
                    }];
}

- (void)_loadJhMoreData
{
    _jhDataList = [NSMutableArray array];
    [self loadJhData];
}

- (void)loadJhDataFinished:(NSDictionary *)result
{
    NSArray *array = [result objectForKey:@"list"];
    if (array.count < 10) {
        self.jhTbView.isMore = NO;
    }else
    {
        self.jhTbView.isMore = YES;
    }
    NSMutableArray *mArray = [[NSMutableArray alloc] initWithCapacity:array.count];
    for (NSDictionary *dic in array) {
        GiftCellModel *model = [[GiftCellModel alloc] initContentWith:dic];
        [mArray addObject:model];
    }
    [_jhDataList addObjectsFromArray:mArray];
    self.jhTbView.dataList = self.jhDataList;
    [self.jhTbView reloadData];
    [self.jhTbView doneLoadingTableViewData];
}

- (void)loadJfData
{
    //http://m.a.ewoka.com/number/cardlist/0
    NSString *urlStr = [NSString stringWithFormat:@"http://m.a.ewoka.com/number/cardlist/0/%d",self.jfDataList.count/10+1];
    __weak GiftPackViewController *this = self;

    [DataService requestWithURL:urlStr
                         params:nil
                     httpMethod:@"GET"
                    finishBlock:^(id result) {
                        [this loadJfDataFinished:result];
                    }];
    
}

- (void)_loadJfMoreData
{
    _jfDataList = [NSMutableArray array];
    [self loadJfData];
}

- (void)loadJfDataFinished:(NSDictionary *)result
{
    NSArray *array = [result objectForKey:@"list"];
    if (array.count < 10) {
        self.jfTbView.isMore = NO;
    }else
    {
        self.jfTbView.isMore = YES;
    }
    NSMutableArray *mArray = [[NSMutableArray alloc] initWithCapacity:array.count];
    for (NSDictionary *dic in array) {
        GiftCellModel *model = [[GiftCellModel alloc] initContentWith:dic];
        [mArray addObject:model];
    }
    [_jfDataList addObjectsFromArray:mArray];
    self.jfTbView.dataList = self.jfDataList;
    [self.jfTbView reloadData];
    [self.jfTbView doneLoadingTableViewData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)xsBtnClick:(UIButton *)sender {
    self.arrowImgView.left = sender.left + sender.width/2 - self.arrowImgView.width/2;
    UIView *backView = [self.changeView viewWithTag:10];
    [self.changeView bringSubviewToFront:backView];
    backView.left = 0;
    
}

- (IBAction)jhBtnClick:(UIButton *)sender {
    self.arrowImgView.left = sender.left + sender.width/2 - self.arrowImgView.width/2;
    UIView *backView = [self.changeView viewWithTag:20];
    [self.changeView bringSubviewToFront:backView];
    backView.left = 0;
}

- (IBAction)jfBtn:(UIButton *)sender {
    self.arrowImgView.left = sender.left + sender.width/2 - self.arrowImgView.width/2;
    UIView *backView = [self.changeView viewWithTag:30];
    [self.changeView bringSubviewToFront:backView];
    backView.left = 0;
}

#pragma mark - BaseTableView delegate
//上拉加载的协议方法
- (void)pullUp:(BaseTableView *)tableView {
    if (tableView.tag == 10) {
        NSString *str = [(UIButton *)[tableView valueForKey:@"moreButton"] titleLabel].text;
        if (![str isEqualToString:@"加载更多数据"]) {
            return;
        }else
        {
            [self loadXsData];
        }

    }else if (tableView.tag == 20)
    {
        NSString *str = [(UIButton *)[tableView valueForKey:@"moreButton"] titleLabel].text;
        if (![str isEqualToString:@"加载更多数据"]) {
            return;
        }else
        {
            [self loadJhData];
        }
    }else if (tableView.tag == 30)
    {
        NSString *str = [(UIButton *)[tableView valueForKey:@"moreButton"] titleLabel].text;
        if (![str isEqualToString:@"加载更多数据"]) {
            return;
        }else
        {
            [self loadJfData];
        }
    }
}
@end
